using System;
using System.Data;
using System.Configuration;
using Pheonix.Fashionzoomer.DAL;
using System.Data.SqlClient;
using System.Collections;
using Pheonix.CodeLibrary.Data;
using Pheonix.CodeLibrary;

/// <summary>
/// Summary description for Membership
/// </summary>

namespace Pheonix.Fashionzoomer.Entity.SqlServer2000
{
    [System.ComponentModel.DataObject]
    public class Membership : SqlServerDAO
    {
        #region Membership Private Fields

		private Int32 _id;
		private String _packageName;
		private DateTime _expireDate;
		private Decimal _packageCost;

        #endregion

        #region Membership Constractors

		public Membership(){}

		public Membership
		(
			Int32 id,
			String packageName,
			DateTime expireDate,
			Decimal packageCost

		)
		{
			this._id = id;
			this._packageName = packageName;
			this._expireDate = expireDate;
			this._packageCost = packageCost;
		}

        #endregion

        #region Membership Properties

		public Int32 Id
		{
			get{ return this._id; }
			set{ this._id = value; }
		}

		public String PackageName
		{
			get{ return this._packageName; }
			set{ this._packageName = value; }
		}

		public DateTime ExpireDate
		{
			get{ return this._expireDate; }
			set{ this._expireDate = value; }
		}

		public Decimal PackageCost
		{
			get{ return this._packageCost; }
			set{ this._packageCost = value; }
		}


        #endregion

        #region Membership Static Methods

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
        public static Int32 CreateNewMembership
        (
			String packageName,
			DateTime expireDate,
			Decimal packageCost

        )
        {
            //creating an empty Membership object
            Membership membership = new Membership();
            membership.PackageName = packageName;
			membership.ExpireDate = expireDate;
			membership.PackageCost = packageCost;

            return Membership.CreateNewMembership(membership);
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public static bool UpdateMembership
        (
			Int32 id,
			String packageName,
			DateTime expireDate,
			Decimal packageCost

        )
        {
            Membership membership = Membership.GetMembershipById(id);
            membership.PackageName = packageName;
			membership.ExpireDate = expireDate;
			membership.PackageCost = packageCost;

            return Membership.UpdateMembership(membership);
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public static bool DeleteMembership
        (
            Int32 id
        )
        {
            //Validate Input
            if (id <= GetIdMinValue)
                throw (new ArgumentOutOfRangeException("id in Membership"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddParameterToSqlCmd(sqlCmd, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, id);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_MEMBERSHIP_DELETEMEMBERSHIP);
            GetOneData(sqlCmd);
            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return (returnValue == 0 ? false : true);

        }

        public static bool DeleteMembershipList(System.Collections.Generic.List<Int32> idListToDelete)
        {
            //Validate Input
            foreach (Int32 id in idListToDelete)
                if (id <= GetIdMinValue)
                    throw (new ArgumentOutOfRangeException("id in Membership"));

            //performing deletion operation //

            string csvString = Utility.MakeCSV(idListToDelete);

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddParameterToSqlCmd(sqlCmd, "@IdList", SqlDbType.NVarChar, csvString.Length, 
                ParameterDirection.Input, csvString);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_MEMBERSHIP_DELETEMEMBERSHIPLIST);
            GetOneData(sqlCmd);
            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return (returnValue == idListToDelete.Count ? true : false);
        }
		
        #endregion

        #region Membership Instance Methods
        
        public bool Save()
		{
			if(Id <= GetIdMinValue)
			{
				Int32 TempId=CreateNewMembership(this);
				if(TempId>0)
				{
					_id=TempId;
					return true;
				}
				else
				{
					// error occurs
					return false;
				}
			}
			else
			{	//This is a update Command
				return (UpdateMembership(this));
			}
		}

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static CustomCollection<Membership> GetAllMembership()
        {
            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_MEMBERSHIP_GETALLMEMBERSHIP);
            GenerateCollectionFromReader result = new GenerateCollectionFromReader(GenerateMembershipCollectionFromReader);
            CustomCollection<Membership> objCollection = ((CustomCollection<Membership>)GetAllData(sqlCmd, result));

            return objCollection;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static CustomCollection<Membership> GetAllMembershipPaged(String orderBy, Int32 startRowIndex, Int32 maximumRows)
        {
            //Validate Input
            if (orderBy == "")
                orderBy = "Id";

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@orderby", SqlDbType.VarChar, 50, ParameterDirection.Input, orderBy);
            AddParameterToSqlCmd(sqlCmd, "@startRowIndex", SqlDbType.Int, 0, ParameterDirection.Input, startRowIndex);
            AddParameterToSqlCmd(sqlCmd, "@maximumRows", SqlDbType.Int, 0, ParameterDirection.Input, maximumRows);

            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_MEMBERSHIP_GETALLMEMBERSHIPPAGED);

            GenerateCollectionFromReader result = new GenerateCollectionFromReader(GenerateMembershipCollectionFromReader);
            CustomCollection<Membership> objCollection = ((CustomCollection<Membership>)GetAllData(sqlCmd, result));
            return objCollection;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static int GetAllMembershipPagedCount(String orderBy, Int32 startRowIndex, Int32 maximumRows)
        {
            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();

            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_MEMBERSHIP_GETALLMEMBERSHIPPAGEDCOUNT);
            GetOneData(sqlCmd);

            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return returnValue;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static Membership GetMembershipById(Int32 id)
        {
            if (id <= GetIdMinValue)
                throw (new ArgumentOutOfRangeException("id in Membership"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, id);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_MEMBERSHIP_GETMEMBERSHIPBYID);
            GenerateCollectionFromReader result = new GenerateCollectionFromReader(GenerateMembershipCollectionFromReader);
            CustomCollection<Membership> objCollection = ((CustomCollection<Membership>)GetAllData(sqlCmd, result));

            if (objCollection.Count > 0)
                return (Membership)objCollection[0];
            else
                return null;
        }

        public static Int32 CreateNewMembership(Membership newMembership)
        {
            // validate Parameters
            if (newMembership == null)
                throw (new ArgumentNullException("newMembership in Membership"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);

            AddParameterToSqlCmd(sqlCmd, "@PackageName", SqlDbType.NVarChar, 64, ParameterDirection.Input, newMembership.PackageName);
			AddParameterToSqlCmd(sqlCmd, "@ExpireDate", SqlDbType.DateTime, 0, ParameterDirection.Input, newMembership.ExpireDate);
			AddParameterToSqlCmd(sqlCmd, "@PackageCost", SqlDbType.Decimal, 0, ParameterDirection.Input, newMembership.PackageCost);

            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_MEMBERSHIP_CREATENEWMEMBERSHIP);
            GetOneData(sqlCmd);
            return ((Int32)sqlCmd.Parameters["@ReturnValue"].Value);

        }

        public static bool UpdateMembership(Membership newMembership)
        {
            // validate Parameters
            if (newMembership == null)
                throw (new ArgumentNullException("newMembership"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);

            AddParameterToSqlCmd(sqlCmd, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, newMembership.Id);
            AddParameterToSqlCmd(sqlCmd, "@PackageName", SqlDbType.NVarChar, 64, ParameterDirection.Input, newMembership.PackageName);
			AddParameterToSqlCmd(sqlCmd, "@ExpireDate", SqlDbType.DateTime, 0, ParameterDirection.Input, newMembership.ExpireDate);
			AddParameterToSqlCmd(sqlCmd, "@PackageCost", SqlDbType.Decimal, 0, ParameterDirection.Input, newMembership.PackageCost);

            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_MEMBERSHIP_UPDATEMEMBERSHIP);
            GetOneData(sqlCmd);
            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return (returnValue == 0 ? false : true);
        }

        #endregion

        #region Membership Related Collections
        /// <summary>
        /// Creates and returns a strongly typed collection of Membership custom entity. 
        /// The colelction is created through iterating on the IdataReader object which contains 
        /// Membership information, as a set of records, similar to tabular format.
        /// </summary>
        public static IList GenerateMembershipCollectionFromReader(IDataReader returnData)
        {

            //creating the instance of Membership collection
            CustomCollection<Membership> colMembership = new CustomCollection<Membership>();

            //Iterating thriugh the data reader, to generate Membership collection.

            //getting the Membership collection by iterating thru the data reader
            while (returnData.Read())
            {
                //passing the Membership constructor parameters from the current instance of data reader fields.
                Membership newMembership = new Membership
                (
                    returnData["id"] == System.DBNull.Value ? GetIdMinValue : (Int32)returnData["id"],
                    returnData["package_name"] == System.DBNull.Value ? String.Empty : (String)returnData["package_name"],
					returnData["expire_date"] == System.DBNull.Value ? DateTime.MinValue : (DateTime)returnData["expire_date"],
					returnData["package_cost"] == System.DBNull.Value ? Decimal.MinValue : (Decimal)returnData["package_cost"]

                );
                //adding the Membership to the collection
                colMembership.Add(newMembership);
            }

            //returns the collection of Membership 
            return (colMembership);
        }

        #endregion

        #region Constants and Default Values

        public static Int32 GetIdMinValue
        {
            get
            {
                return 0;
            }
        }

        //contants
        internal const string SPR_MEMBERSHIP_CREATENEWMEMBERSHIP = "spr_Membership_CreateNewMembership";
        internal const string SPR_MEMBERSHIP_UPDATEMEMBERSHIP = "spr_Membership_UpdateMembership";
        internal const string SPR_MEMBERSHIP_DELETEMEMBERSHIP = "spr_Membership_DeleteMembership";
        internal const string SPR_MEMBERSHIP_DELETEMEMBERSHIPLIST = "spr_Membership_DeleteMembershipList";
        internal const string SPR_MEMBERSHIP_GETMEMBERSHIPBYID = "spr_Membership_GetMembershipById";
        internal const string SPR_MEMBERSHIP_GETALLMEMBERSHIP = "spr_Membership_GetAllMembership";
        internal const string SPR_MEMBERSHIP_GETALLMEMBERSHIPPAGED = "spr_Membership_GetAllMembershipPaged";
        internal const string SPR_MEMBERSHIP_GETALLMEMBERSHIPPAGEDCOUNT = "spr_Membership_GetAllMembershipPagedCount";

		//Foreign key contants
		
        #endregion
    }
}
